package com.lzh.others.java8.book.chapter_3_3常用流操作;


import java.util.List;
import java.util.stream.Stream;

import static com.lzh.others.java8.book.AssertEqualsUtil.assertEquals;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

/**
 * flatMap操作的是一个一个的Stream,可以将多个stream进行操作 汇聚成一个新的
 *
 * @author lzh
 */
public class flatMap {
    public static void main(String[] args) {
        //将两个流合并
        List<Integer> together = Stream.of(asList(1, 5), asList(3, 4))
                .flatMap(numbers -> numbers.stream())
                .collect(toList());

        //将两个流合并，期间将不是1，2，3，4的数字删除，并将最终集合去重
        List<Integer> together1 = Stream.of(asList(1, 1, 1, 2, 3, 5, 10, 11), asList(3, 4))
                .flatMap(numbers -> numbers.stream()
                        .filter(value -> asList(1, 2, 3, 4).contains(value))
                ).distinct()
                .collect(toList());

        assertEquals(asList(1, 2, 3, 4), together);
        assertEquals(asList(1, 2, 3, 4), together1);
    }

}
